const express = require('express')
const router = express.Router()
const db = require('../databases/index')
const multer = require('multer')
// 配置中间件
const upload = multer({ dest: 'uploads/' })
// 时间
const moment = require('moment')
// 局部使用中间件
router.post('/add', upload.single('cover_img'), (req, res) => {
    // 文件上传要单独设置需要formdata中间件
    let { title, cate_id, content, state } = req.body
    // 添加文件夹更加方便管理 结构清晰
    let cover_img = '/uploads/' + req.file.filename
    let pub_date = moment().format('YYYY-MM-DD HH:mm:ss'); 
    // let pub_date = 'YYYY-MM-DD HH:mm:ss'
    let author_id = req.user.id
    let is_delete = 0
    let sql = `insert into article set title="${title}",cate_id=${cate_id},content="${content}",state="${state}", cover_img="${cover_img}", author_id="${author_id}", pub_date="${pub_date}", is_delete="${is_delete}" `
    // let sql = `insert into article set title="${title}" , cate_id=${cate_id} ,content="${content}", state="${state}" ,cover_img="${cover_img}", author_id=${author_id} ,pub_date="${pub_date}", is_delete="${is_delete}"  `



    db(sql, (err, results) => {
        if (err) {
            return err
        }
        if (results.affectedRows !== 1) {
            return res.send({
                status: 1,
                message: '发布文章失败！'
            })
        }
        res.send({
            status: 0,
            message: '发布文章成功！'
        })

    })
})



module.exports = router